package com.spzx.product.service.impl;

/**
 * @Author GQS @Date 2024/9/20/18:38 @Description
 */
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.CategoryService;
import com.spzx.product.service.ProductSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品规格Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec>
    implements ProductSpecService {

  @Autowired private ProductSpecMapper productSpecMapper;
  @Autowired private CategoryService categoryService;

  /**
   * 查询商品规格列表
   *
   * @param productSpec 商品规格
   * @return 商品规格
   */
  @Override
  public List<ProductSpec> selectProductSpecList(ProductSpec productSpec) {
    // 方法一 通用service方法分步查询封装数据
    List<ProductSpec> productSpecList =
        this.list(
            Wrappers.lambdaQuery(ProductSpec.class)
                .eq(
                    productSpec.getCategoryId() != null,
                    ProductSpec::getCategoryId,
                    productSpec.getCategoryId())
                .like(
                    productSpec.getSpecName() != null,
                    ProductSpec::getSpecName,
                    productSpec.getSpecName()));
    for (ProductSpec spec : productSpecList) {
      // 获取分类名称
      Category category = categoryService.getById(spec.getCategoryId());
      spec.setCategoryName(category.getName());
      // 获取分类id列表
      // 获取当前品牌的分类ID对应的全部父分类ID列表
      List<Long> categoryIdList = categoryService.getAllCategoryIdList(spec.getCategoryId());
      spec.setCategoryIdList(categoryIdList);
    }
    return productSpecList;

    // 方法二 自定义SQL
    // return productSpecMapper.selectProductSpecList(productSpec);

  }

  /**
   * 查询商品规格
   *
   * @param id 商品规格主键
   * @return 商品规格
   */
  @Override
  public ProductSpec selectProductSpecById(Long id) {
    ProductSpec productSpec = productSpecMapper.selectById(id);
    // 获取分类名称
    Category category = categoryService.getById(productSpec.getCategoryId());
    productSpec.setCategoryName(category.getName());
    // 获取分类id列表
    List<Long> categoryIdList = categoryService.getAllCategoryIdList(productSpec.getCategoryId());
    productSpec.setCategoryIdList(categoryIdList);
    return productSpec;
  }

  @Override
  public List<ProductSpec> selectProductSpecListByCategoryId(Long categoryId) {
    return productSpecMapper.selectList(
        Wrappers.lambdaQuery(ProductSpec.class).eq(ProductSpec::getCategoryId, categoryId));
  }
}
